<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>种植员界面</title>
    <style>
        /* 全局样式 */
        body {
            font-family: "Arial", sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(to bottom right, #e0f7fa, #00695c);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            display: flex;
            height: 90%;
            width: 90%;
            background: #ffffff;
            border-radius: 12px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            position: relative;
        }

        /* 左侧导航栏 */
        .sidebar {
            background-color: #004d40;
            color: white;
            width: 240px;
            padding: 20px;
            display: flex;
            flex-direction: column;
        }

        .sidebar h2 {
            font-size: 22px;
            text-align: center;
            margin-bottom: 20px;
            color: #e0f7fa;
            font-weight: bold; /* 加粗 */
        }

        .sidebar a {
            color: #e0f7fa;
            text-decoration: none;
            margin: 10px 0;
            font-size: 18px;
            padding: 12px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold; /* 加粗 */
        }

        .sidebar a:hover {
            background-color: #00796b;
            color: #ffffff;
        }

        /* 右侧内容区域 */
        .main-content {
            flex: 1;
            padding: 20px;
            background-color: #f9f9f9;
            overflow-y: auto;
            position: relative;
        }

        .main-content h3 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #004d40;
            font-weight: bold; /* 加粗 */
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            background-color: rgba(255, 255, 255, 0.9); /* 半透明背景色 */
            border-radius: 8px;
            overflow: hidden;
            position: relative;
        }

        th, td {
            padding: 12px;
            border: 1px solid #e0e0e0;
            text-align: center;
            font-size: 16px;
            font-weight: bold; /* 加粗 */
        }

        th {
            background-color: #004d40;
            color: white;
        }

        tr:nth-child(even) {
            background-color: rgba(240, 240, 240, 0.9); /* 半透明背景 */
        }

        .btn {
            padding: 8px 16px;
            font-size: 14px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold; /* 加粗 */
        }

        .btn.edit {
            background-color: #2196f3;
            color: white;
        }

        .btn.edit:hover {
            background-color: #1976d2;
        }

        .btn.delete {
            background-color: #f44336;
            color: white;
        }

        .btn.delete:hover {
            background-color: #d32f2f;
        }

        /* 底部背景图片 */
        .main-content::after {
            content: "";
            position: absolute;
            top: 0; /* 从顶部开始 */
            left: 0;
            right: 0;
            bottom: 0; /* 填充整个区域 */
            background: url('../static/img/装饰.png') no-repeat center;
            background-size: cover; /* 让图片完全覆盖 */
            opacity: 0.3; /* 图片透明度降低，防止影响阅读 */
            pointer-events: none; /* 禁止干扰鼠标操作 */
        }

        /* 弹窗样式 */
        .form-popup {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 30px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
            border-radius: 10px;
            z-index: 1000;
        }

        .form-popup input, .form-popup textarea, .form-popup button, .form-popup select {
            margin: 12px 0;
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
        }

        .form-popup button {
            background-color: #00796b;
            color: white;
            border: none;
            cursor: pointer;
            font-weight: bold; /* 加粗 */
        }

        .form-popup button:hover {
            background-color: #004d40;
        }

        .form-popup .cancel-btn {
            background-color: #f44336;
            color: white;
            font-weight: bold; /* 加粗 */
        }

        .form-popup .cancel-btn:hover {
            background-color: #d32f2f;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 左侧导航栏 -->
        <div class="sidebar">
            <h2>种植员界面</h2>
            <a href="#" onclick="loadCrops()">作物信息</a>
            <a href="#" onclick="loadLogs()">日志信息</a>
        </div>

        <!-- 右侧内容区域 -->
        <div class="main-content" id="content">
            <h3>作物信息</h3>
            <button class="btn add" onclick="prepareAddCropForm()">添加作物</button>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>作物名称</th>
                        <th>种植日期</th>
                        <th>位置</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td>水稻</td>
                        <td>2024-12-01</td>
                        <td>温室1号</td>
                        <td>
                            <button class="btn edit">编辑</button>
                            <button class="btn delete">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 弹窗 -->
    <div class="form-popup" id="form-popup">
        <h3 id="form-title">编辑作物信息</h3>
        <form id="edit-form">
            <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
            <div id="form-fields">
                <!-- 动态生成表单字段 -->
            </div>
            <button type="submit">提交</button>
            <button type="button" class="cancel-btn" onclick="closeForm()">取消</button>
        </form>
    </div>

    <script>
        let currentView = '';

        function showForm(title, fieldsHTML) {
            document.getElementById('form-title').innerText = title;
            document.getElementById('form-fields').innerHTML = fieldsHTML;
            document.getElementById('form-popup').style.display = 'block';
        }

        function closeForm() {
            document.getElementById('form-popup').style.display = 'none';
        }

        function loadCrops() {
            currentView = 'crop';
            fetch('/get_crops/')
                .then(response => response.json())
                .then(data => {
                    let tableHTML = `
                        <h2>作物信息列表</h2>
                        <button onclick="prepareAddCropForm()">添加作物信息</button>
                        <table>
                            <tr>
                                <th>ID</th>
                                <th>作物名称</th>
                                <th>作物类型</th>
                                <th>种植日期</th>
                                <th>位置</th>
                                <th>操作</th>
                            </tr>
                    `;
                    data.data.forEach(crop => {
                        tableHTML += `
                            <tr>
                                <td>${crop.id}</td>
                                <td>${crop.name}</td>
                                <td>${crop.crop_type}</td>
                                <td>${crop.planting_date}</td>
                                <td>${crop.location}</td>
                                <td>
                                    <button onclick="editCrop(${crop.id})">编辑</button>
                                    <button onclick="deleteCrop(${crop.id})">删除</button>
                                </td>
                            </tr>
                        `;
                    });
                    tableHTML += `</table>`;
                    document.getElementById('content').innerHTML = tableHTML;
                })
                .catch(error => console.error("加载作物信息出错：", error));
        }

        function loadLogs() {
            currentView = 'log';
            fetch('/get_logs/')
                .then(response => response.json())
                .then(data => {
                    let tableHTML = `
                        <h2>日志信息列表</h2>
                        <button onclick="prepareAddLogForm()">添加日志信息</button>
                        <table>
                            <tr>
                                <th>ID</th>
                                <th>种植日期</th>
                                <th>数量</th>
                                <th>位置</th>
                                <th>作物名称</th>
                                <th>操作</th>
                            </tr>
                    `;
                    data.data.forEach(log => {
                        tableHTML += `
                            <tr>
                                <td>${log.id}</td>
                                <td>${log.planting_date}</td>
                                <td>${log.quantity}</td>
                                <td>${log.location}</td>
                                <td>${log.crop__name}</td>
                                <td>
                                    <button onclick="editLog(${log.id})">编辑</button>
                                    <button onclick="deleteLog(${log.id})">删除</button>
                                </td>
                            </tr>
                        `;
                    });
                    tableHTML += `</table>`;
                    document.getElementById('content').innerHTML = tableHTML;
                })
                .catch(error => console.error("加载日志信息出错：", error));
        }

        function prepareAddCropForm() {
            const fields = `
                <input type="text" name="name" placeholder="作物名称" required>
                <input type="text" name="crop_type" placeholder="作物类型" required>
                <input type="date" name="planting_date" placeholder="种植日期" required>
                <input type="text" name="location" placeholder="位置" required>
            `;
            showForm("添加作物信息", fields);
            document.getElementById('edit-form').onsubmit = addCrop;
        }

        function prepareAddLogForm() {
            const fields = `
                <input type="date" name="planting_date" placeholder="种植日期" required>
                <input type="number" name="quantity" placeholder="数量" required>
                <input type="text" name="location" placeholder="位置" required>
                <input type="number" name="crop" placeholder="作物ID" required>
            `;
            showForm("添加日志信息", fields);
            document.getElementById('edit-form').onsubmit = addLog;
        }

        function addCrop(event) {
            event.preventDefault();
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
            const formData = new FormData(event.target);
            const data = Object.fromEntries(formData.entries());

            fetch('/add_crop/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify(data)
            }).then(() => {
                closeForm();
                loadCrops();
            });
        }

        function addLog(event) {
            event.preventDefault();
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
            const formData = new FormData(event.target);
            const data = Object.fromEntries(formData.entries());

            fetch('/add_log/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify(data)
            }).then(() => {
                closeForm();
                loadLogs();
            });
        }

        function editCrop(id) {
            fetch(`/get_crop/${id}/`)
                .then(response => response.json())
                .then(crop => {
                    const fields = `
                        <input type="text" name="name" value="${crop.name}" required>
                        <input type="text" name="crop_type" value="${crop.crop_type}" required>
                        <input type="date" name="planting_date" value="${crop.planting_date}" required>
                        <input type="text" name="location" value="${crop.location}" required>
                    `;
                    showForm("编辑作物信息", fields);
                    document.getElementById('edit-form').onsubmit = function(event) {
                        event.preventDefault();
                        updateCrop(id);
                    };
                });
        }

        function updateCrop(id) {
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
            const formData = new FormData(document.getElementById('edit-form'));
            const data = Object.fromEntries(formData.entries());

            fetch(`/update_crop/${id}/`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify(data)
            }).then(() => {
                closeForm();
                loadCrops();
            });
        }

        function editLog(id) {
            fetch(`/get_log/${id}/`)
                .then(response => response.json())
                .then(log => {
                    const fields = `
                        <input type="date" name="planting_date" value="${log.planting_date}" required>
                        <input type="number" name="quantity" value="${log.quantity}" required>
                        <input type="text" name="location" value="${log.location}" required>
                        <input type="number" name="crop" value="${log.crop}" required>
                    `;
                    showForm("编辑日志信息", fields);
                    document.getElementById('edit-form').onsubmit = function(event) {
                        event.preventDefault();
                        updateLog(id);
                    };
                });
        }

        function updateLog(id) {
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
            const formData = new FormData(document.getElementById('edit-form'));
            const data = Object.fromEntries(formData.entries());

            fetch(`/update_log/${id}/`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify(data)
            }).then(() => {
                closeForm();
                loadLogs();
            });
        }

        function deleteCrop(id) {
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;

            fetch(`/delete_crop/${id}/`, {
                method: 'DELETE',
                headers: { 'X-CSRFToken': csrfToken }
            }).then(() => loadCrops());
        }

        function deleteLog(id) {
            const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;

            fetch(`/delete_log/${id}/`, {
                method: 'DELETE',
                headers: { 'X-CSRFToken': csrfToken }
            }).then(() => loadLogs());
        }

        window.onload = function() {
            loadCrops();
        };
    </script>
</body>
</html>
